<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="平台账号" prop="accountId">
        <el-select v-model="form.accountId" value-key="accountPassword">
          <el-option
            v-for="item in accountList"
            :key="item.id"
            :label="item.platformAccount"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账面消费" prop="bookConsumption">
        <el-input v-model.trim="form.bookConsumption" />
      </el-form-item>
      <el-form-item label="展现" prop="unfold">
        <el-input v-model.trim="form.unfold" />
      </el-form-item>
      <el-form-item label="点击" prop="click">
        <el-input v-model.trim="form.click" />
      </el-form-item>
      <el-form-item label="私信开口量" prop="privateOpenNum">
        <el-input v-model.trim="form.privateOpenNum" />
      </el-form-item>
      <el-form-item label="私信咨询量" prop="privateConsultNum">
        <el-input v-model.trim="form.privateConsultNum" />
      </el-form-item>
      <el-form-item label="私信人数" prop="privateNum">
        <el-input v-model.trim="form.privateNum" />
      </el-form-item>
      <el-form-item label="私现消费" prop="realizeConsumption">
        <el-input v-model.trim="form.realizeConsumption" />
      </el-form-item>
      <el-form-item label="登记客资" prop="signGuestCapital">
        <el-input v-model.trim="form.signGuestCapital" />
      </el-form-item>
      <el-form-item label="评论客资" prop="reviewGuestCapital">
        <el-input v-model.trim="form.reviewGuestCapital" />
      </el-form-item>
      <el-form-item label="登记总数" prop="registration">
        <el-input v-model.trim="form.registration" />
      </el-form-item>
      <el-form-item label="有效客资" prop="canGuestCapital">
        <el-input v-model.trim="form.canGuestCapital" />
      </el-form-item>
      <el-form-item label="加v数量" prop="addWechatNum">
        <el-input v-model.trim="form.addWechatNum" />
      </el-form-item>
      <el-form-item label="新客成交数" prop="newSuccessfulNum">
        <el-input v-model.trim="form.newSuccessfulNum" />
      </el-form-item>
      <el-form-item label="新客成交额" prop="newSuccessfulPrice">
        <el-input v-model.trim="form.newSuccessfulPrice" />
      </el-form-item>
      <el-form-item label="老客成单数" prop="oldSuccessfulNum">
        <el-input v-model.trim="form.oldSuccessfulNum" />
      </el-form-item>
      <el-form-item label="老客成交额" prop="oldSuccessfulPrice">
        <el-input v-model.trim="form.oldSuccessfulPrice" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    editInformationFlow,
    addInformationFlow,
  } from '@/api/informationFlow'
  import { fetchaccountOperate } from '@/api/accountManagement'
  export default defineComponent({
    name: 'UserManagementEdit',
    props: {
      roles: {
        type: Array,
        default: () => {
          return []
        },
      },
    },
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          roleIds: [],
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          mobile: [
            { required: true, trigger: 'blur', message: '请输入手机号' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          roleIds: [{ required: true, trigger: 'blur', message: '请选择角色' }],
          nickname: [
            { required: true, trigger: 'blur', message: '请输入昵称' },
          ],
          gender: [{ required: true, trigger: 'blur', message: '请选择性别' }],
          status: [
            { required: true, trigger: 'change', message: '请选择状态' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        accountList: [],
        fetchAccount: false,
      })
      const fetchAccount = async () => {
        try {
          state.listLoading = true
          const { data: data } = await fetchaccountOperate()
          state.accountList = data
          state.listLoading = false
        } catch (e) {
          console.log(e)
        }
      }
      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = JSON.parse(JSON.stringify(row))
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            try {
              const { msg } =
                state.title === '添加'
                  ? await addInformationFlow(state.form)
                  : await editInformationFlow(state.form)
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              emit('fetch-data')
              close()
            } catch (err) {
              console.error(err)
            }
          }
        })
      }
      fetchAccount()
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
